package statistic;

import database.DatabaseHelper;
import entity.Course;
import entity.Score;
import entity.Section;
import entity.Student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class StudentScoreList {
    public ArrayList<Student> students = new ArrayList<>();
    public ArrayList<Score> scores = new ArrayList<>();

    public enum Order {

        ID, SCORE;

        @Override
        public String toString() {
            switch (this) {
                case ID:
                    return "student.id";
                case SCORE:
                default:
                    return "score.total DESC";
            }
        }

    }

    private StudentScoreList(String where, Order order, Object... params) throws SQLException {
        DatabaseHelper db = new DatabaseHelper();
        String query = String.format("select sec_id,stu_id from section join score on section.id = score.sec_id join student on student.id = score.stu_id where %s order by %s", where, order);
        ResultSet rs = db.query(query, params);
        while (rs.next()) {
            Section sec = Section.find(db, rs.getString(1));
            Student stu = Student.find(db, rs.getString(2));
            students.add(stu);
            scores.add(Score.find(db, sec, stu));
        }
        rs.close();
        db.close();
    }

    public StudentScoreList(Course cou, Order order) throws SQLException {
        this("cou_id = ?", order, cou.getId());
    }

    public StudentScoreList(Course cou, String semester, Order order) throws SQLException {
        this("cou_id = ? and semester = ?", order, cou.getId(), semester);
    }

    public StudentScoreList(Section sec, Order order) throws SQLException {
        this("sec_id = ?", order, sec.getId());
    }
}
